#include "sqlitesd.h"

#include <QObject>
#include <QTabWidget>
#include <QGroupBox>
#include <QFormLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QDebug>
#include <QApplication>

#include "../../ui/openlineedit.h"

#include "../../core/core.h"
#include "../../core/settingssql.h"

class SQLiteSD::SQLiteSDPrivate {
public:
	SQLiteSDPrivate();
	~SQLiteSDPrivate();

	QTabWidget *m_pTab;


	OpenLineEdit *m_pDBName;
	OpenLineEdit *m_pPrefix;
};

SQLiteSD::SQLiteSDPrivate::SQLiteSDPrivate(){
	m_pTab = NULL;
}

SQLiteSD::SQLiteSDPrivate::~SQLiteSDPrivate(){
	delete m_pTab;
}



//end of d-class part
//------------------------------------------------------------------------------
SQLiteSD::SQLiteSD() :
	d(new SQLiteSDPrivate())
{

}

SQLiteSD::~SQLiteSD(){
	delete d;
	Q_CLEANUP_RESOURCE(sqliteres);
}

QString SQLiteSD::category() const {
	return QObject::tr("SQLite");
}

QIcon SQLiteSD::categoryIcon() const {
	Q_INIT_RESOURCE(sqliteres);
	return QIcon(":/settings/res/sqlite.ico");
}

QTabWidget* SQLiteSD::createPages(){
	if( d->m_pTab )
		return d->m_pTab;



	d->m_pTab = new QTabWidget();


	QWidget *general = new QWidget( d->m_pTab );
	d->m_pTab->addTab( general, QObject::tr("Główne") );


	//baza
	//---------
	QGroupBox *group2 = new QGroupBox( QObject::tr("Baza"), general );
	QFormLayout *layout2 = new QFormLayout( group2 );

	d->m_pDBName = new OpenLineEdit( group2 );
	d->m_pDBName->setButtonMode( OpenLineEdit::SaveFile );
	QVariant setting =
			Core::instance()->settings()->value( "plugin/SQLite/database",
												 QVariant() );
	if( !setting.isValid() ){
		QString application = qApp->applicationName();
#if defined(Q_WS_WIN)
		setting = QVariant( QString("%1/%2/localdb.sqlite").
						arg( QString(qgetenv("APPDATA")),
							 application.toLower()) );
#else
		setting = QVariant( QString("%1/.%2/localdb.sqlite").
						arg( QString(getenv("HOME")),
							 application.toLower()) );
#endif
	}
	d->m_pDBName->setText( setting.toString() );
	d->m_pDBName->setToolTip( QObject::tr("To pole nie może być puste\n"
										  "Jeśli tak się stanie, zostanie "
										  "przywrócona ostatnia wartość") );

	d->m_pPrefix = new OpenLineEdit( group2 );
	d->m_pPrefix->setText(
			Core::instance()->settings()->value( "plugin/SQLite/prefix",
												 QVariant("beaver_") )
			.toString() );


	layout2->addRow(QObject::tr("Plik bazy:"), d->m_pDBName);
	layout2->addRow(QObject::tr("Prefix:"), d->m_pPrefix);


	group2->setLayout( layout2 );


	general->setLayout( new QVBoxLayout(general) );
	general->layout()->addWidget(group2);


	return d->m_pTab;
}

void SQLiteSD::save(){
	if( d->m_pDBName->isModified() ){
		if( !d->m_pDBName->text().isEmpty() ){
			Core::instance()->settings()->setValue("plugin/SQLite/database",
												   d->m_pDBName->text() );
		}else{
			QVariant setting =
					Core::instance()->settings()->value(
							"plugin/SQLite/database", QVariant() );
			if( !setting.isValid() ){
				QString application = qApp->applicationName();
#if defined(Q_WS_WIN)
				setting = QVariant( QString("%1/%2/localdb.sqlite").
									arg( QString(qgetenv("APPDATA")),
										 application.toLower()) );
#else
				setting = QVariant( QString("%1/.%2/localdb.sqlite").
									arg( QString(getenv("HOME")),
										 application.toLower()) );
#endif
			}
			d->m_pDBName->setText( setting.toString() );
		}
	}

	if( d->m_pPrefix->isModified() ){
		Core::instance()->settings()->setValue( "plugin/SQLite/prefix",
												d->m_pPrefix->text() );
	}


	d->m_pPrefix->setModified( false );
	d->m_pDBName->setModified( false );
}

void SQLiteSD::reset(){


	QVariant setting =
			Core::instance()->settings()->value( "plugin/SQLite/database",
												 QVariant() );
	if( !setting.isValid() ){
		QString application = qApp->applicationName();
#if defined(Q_WS_WIN)
		setting = QVariant( QString("%1/%2/localdb.sqlite").
							arg( QString(qgetenv("APPDATA")),
								 application.toLower()) );
#else
		setting = QVariant( QString("%1/.%2/localdb.sqlite").
							arg( QString(getenv("HOME")),
								 application.toLower()) );
#endif
	}
	d->m_pDBName->setText( setting.toString() );


	d->m_pPrefix->setText(
			Core::instance()->settings()->value( "plugin/SQLite/prefix",
												 QVariant("beaver_") )
			.toString() );



	d->m_pPrefix->setModified( false );
	d->m_pDBName->setModified( false );
}
